package com.wondertek.mam.tag.tagrep.impl;

import com.wondertek.mam.tag.domain.TagDB;
import com.wondertek.mam.tag.domain.TagWord;
import com.wondertek.mam.tag.tagrep.TagRep;
import com.wondertek.mam.tag.repository.TagRepository;
import com.wondertek.mam.tag.wordSource.bean.NetHotWord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Jpa实现
 * Created by wangdongxu on 2017-09-06 上午 11:08:43.
 */
public class  TagSqlRep implements TagRep {
    private static final Logger LOGGER = LoggerFactory.getLogger(TagSqlRep.class);
    private TagRepository tagRepository;

    public TagSqlRep() {
    }

    public TagSqlRep(TagRepository tagRepository){
        this.tagRepository = tagRepository;
    }

    @Override
    public Collection<TagWord> getTagWordWithScore(List<String> wordsList) {
        List<TagDB> allByTagIn = tagRepository.findAllByTagIn(wordsList);
        if (allByTagIn.size() ==0){
            return new ArrayList<>();
        }
        Map<String, TagWord> resultMap = new HashMap<>();
        allByTagIn
            .stream()
            .map(tagDB -> new TagWord(tagDB.getTag(),tagDB.getScore()))
            .forEach(tagWord -> {
                resultMap.putIfAbsent(tagWord.getWord(),tagWord);
                if(resultMap.get(tagWord.getWord()).getScore()<tagWord.getScore())
                    resultMap.put(tagWord.getWord(),tagWord);
            });
        return resultMap.entrySet()
            .parallelStream()
            .map(Map.Entry::getValue)
            .collect(Collectors.toList());
    }

    @Override
    public void save(NetHotWord netHotWord) {
        if (netHotWord ==null) {
            return;
        }
        LOGGER.debug("词语入库"+netHotWord.getWords().keySet());
        //更新已存在的内容
        List<TagDB> allByTagInAndTop = tagRepository.findAllByTagIn(netHotWord.getWords().keySet())
            .parallelStream()
            .map(tagDB -> {
                if (tagDB.getGroupId()!=null&&tagDB.getGroupId()!=0) return tagDB;
                if (netHotWord.getWords().get(tagDB.getTag())==null)return tagDB;
                if (tagDB.getUpdateTime().toLocalDate().isEqual(LocalDate.now())&&tagDB.getHot()){
                    return tagDB;
                }
                if (tagDB.getProtectedTime().isBefore(LocalDate.now().plusDays(1L))){
                    tagDB.setHot(netHotWord.getWords().get(tagDB.getTag()));
                    tagDB.setScore(netHotWord.getWords().get(tagDB.getTag())?5:4);
                }

                tagDB.setUpdateTime(LocalDateTime.now());
                return tagDB;
            })
            .collect(Collectors.toList());
        tagRepository.save(allByTagInAndTop);
        //增加新内容
        List<String> tagWords = allByTagInAndTop
            .parallelStream()
            .map(TagDB::getTag)
            .collect(Collectors.toList());
        List<TagDB> collect = netHotWord.getWords().entrySet()
            .parallelStream()
            .filter(stringBooleanEntry -> !tagWords.contains(stringBooleanEntry.getKey()))
            .map(stringBooleanEntry -> new TagDB(
                stringBooleanEntry.getKey(),
                //netHotWord.getKeyWord(),
                LocalDateTime.now(),
                LocalDateTime.now(),
                stringBooleanEntry.getValue()?5:4,
                stringBooleanEntry.getValue(),
                LocalDate.now().plusDays(1L),
                -1,
                null)
            )
            .collect(Collectors.toList());
        tagRepository.save(collect);
    }
}
